package com.niu.online.utilstest;
import com.niu.business.annotation.MyLog;
import com.niu.business.model.entity.SysUser;
import com.niu.business.model.mapper.SysUserMapper;
import com.niu.business.utils.ioUtils.IoWriteUtil;
import com.niu.business.utils.redisUtils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
@Controller
@RequestMapping("/testController")
public class TestController {

    @Resource
    private SysUserMapper mapper;
    @Resource
    private IoWriteUtil ioWriteUtil;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    private JdbcTemplate jdbcTemplate;
    @RequestMapping("/writeTxt")
    public void WriteTxt(){

        ioWriteUtil.writeTxt("D:\\1111\\AEF10.txt");
    }
    @RequestMapping("/readTxt")
    public void readTxt(){

        Map map = ioWriteUtil.readTxt("D:\\project\\oursHome\\model\\src\\main\\resources\\AEF10.txt");
        System.out.println("读取完成---开始写入");
        ioWriteUtil.writeTxt("D:\\1111\\aaaa.txt",map);
    }

    @RequestMapping("/transactional")
    private void transactional(){
        insert();
    }


    @RequestMapping("/annotationAop")
    @MyLog("测试注解")
    public void annotationAop(){

    }

    @RequestMapping("/redisSetKey")
    public void redisSetKey(){
        SysUser sysUser = new SysUser();
        sysUser.setUserName("xmx");
        sysUser.setUserPassword("123456");
        sysUser.setUserType("O");
        sysUser.setUserCreateTime(new Date());
        sysUser.setUserUpdateTime(new Date());
        redisUtils.setKey("key3",sysUser);
    }

    @RequestMapping("/linkRedis")
    public void linkRedis(){
        SysUser sysUser = new SysUser();
        sysUser.setUserName("xmx");
        sysUser.setUserPassword("123456");
        sysUser.setUserType("O");
        sysUser.setUserCreateTime(new Date());
        sysUser.setUserUpdateTime(new Date());
        redisUtils.setKey("key2",sysUser);
    }


    @RequestMapping("/redisDelKey")
    public void redisDelKey(){
        redisUtils.delKey("key1");
    }

    @RequestMapping("/redisSetTimeKey")
    public void redisSetTimeKey(){
        redisUtils.expire("key1", 100);
    }

    @RequestMapping("/redisGetKey")
    public void  redisGetKey(){
        System.out.println(redisUtils.getKey("key3"));
    }

    @Transactional
    public void insert(){
//        SysUser sysUser = new SysUser();
//        sysUser.setUserName("xmx");
//        sysUser.setUserPassword("123456");
//        sysUser.setUserType("O");
//        sysUser.setUserCreateTime(new Date());
//        sysUser.setUserUpdateTime(new Date());
//        mapper.insert(sysUser);
//
//        if (1 != 0){
//            try {
//                throw  BusiUtilsException.createException("报错");
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//        }
        jdbcTemplate.execute("INSERT INTO `t_sys_user`( `USER_NAME`, `USER_PASSWORD`, `USER_TYPE`, " +
                "`USER_CREATE_TIME`, `USER_UPDATE_TIME`) VALUES ( 'xmx0012', '123456', 'O', '2022-04-05 19:23:55', '2022-04-05 19:23:55');\n");
        throw new NullPointerException();
//        sysUser.setUserName("xmxx12312");
//        sysUser.setUserId(2);
//        mapper.updateById(sysUser);

    }
}
